﻿@model NewsletterBoxModel
@using Nop.Web.Models.Newsletter;
<div class="newsletter">
    <div class="title">
        <strong>@T("Newsletter.Title")</strong>
    </div>
    <div class="newsletter-subscribe" id="newsletter-subscribe-block">
        <div class="newsletter-email">
            @Html.TextBoxFor(x => x.NewsletterEmail, new { id = "newsletter-email", @class = "newsletter-subscribe-text", placeholder = T("Newsletter.Email.Placeholder") })
            <input type="button" value="@T(Model.AllowToUnsubscribe ? "Newsletter.Options.Send" : "Newsletter.Options.Subscribe")" id="newsletter-subscribe-button" class="button-1 newsletter-subscribe-button" />
            @if (Model.AllowToUnsubscribe)
            {
                <div class="options">
                    <span class="subscribe">
                        <input id="newsletter_subscribe" type="radio" value="newsletter_subscribe" name="newsletter_block" checked="checked" />
                        <label for="newsletter_subscribe">@T("Newsletter.Options.Subscribe")</label>
                    </span>
                    <span class="unsubscribe">
                        <input id="newsletter_unsubscribe" type="radio" value="newsletter_unsubscribe" name="newsletter_block" />
                        <label for="newsletter_unsubscribe">@T("Newsletter.Options.Unsubscribe")</label>
                    </span>
                </div>
            }
        </div>
        <div class="newsletter-validation">
            <span id="subscribe-loading-progress" style="display: none;" class="please-wait">@T("Common.Wait...")</span>
            @Html.ValidationMessageFor(x => x.NewsletterEmail)
        </div>
    </div>
    <div class="newsletter-result" id="newsletter-result-block"></div>
    <script type="text/javascript">
        function newsletter_subscribe(subscribe) {
            var email = $("#newsletter-email").val();
            var subscribeProgress = $("#subscribe-loading-progress");
            subscribeProgress.show();
            $.ajax({
                cache: false,
                type: "POST",
                url: "@(Url.RouteUrl("SubscribeNewsletter"))",
                data: { "subscribe": subscribe, "email": email },
                success: function (data) {
                    subscribeProgress.hide();
                    $("#newsletter-result-block").html(data.Result);
                    if (data.Success) {
                        $('#newsletter-subscribe-block').hide();
                        $('#newsletter-result-block').show();
                    } else {
                        $('#newsletter-result-block').fadeIn("slow").delay(2000).fadeOut("slow");
                    }
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert('Failed to subscribe.');
                    subscribeProgress.hide();
                }
            });
        }

        $(document).ready(function () {
            $('#newsletter-subscribe-button').click(function () {
                @if (Model.AllowToUnsubscribe)
                {
                    <text>if ($('#newsletter_subscribe').is(':checked')) {
                    newsletter_subscribe('true');
                } else {
                    newsletter_subscribe('false');
                }</text>
                }
                else
                {
                    <text>newsletter_subscribe('true');</text>
                }
            });
            $("#newsletter-email").keydown(function (event) {
                if (event.keyCode == 13) {
                    $("#newsletter-subscribe-button").click();
                    return false;
                }
            });
        });
    </script>
</div>
